//给easyui创建扩展属性
var easyui = easyui || {};

/**
 * 封装dialog弹出框
 * @param options
 * @returns {*|$}
 */
easyui.dialog = function (options) {
    var dgWidows = parent.$('<div/>');
    var formObj;
    var formUrl;
    var opts = $.extend({
        title: '新窗口', width: 640, height: 480, modal: true, handler: false, maximizable: true,
        data: [],
        onLoad: function () {
            formObj = $(dgWidows[0]).find('form');
            formUrl = formObj.data('url');
            formObj.form('load', opts.data);
        },
        onClose:function () {
            dgWidows.dialog('destroy');
        },
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function () {

                formObj.form({
                    url: formUrl,

                    onSubmit: function () {
                        // do some check
                        // return false to prevent submit;
                    },
                    success: function (data) {
                        if (typeof data === 'string') data = JSON.parse(data);

                        var code = data.code;
                        var msg = data.msg;

                        if (code === 0) {
                            // success
                            dgWidows.dialog('destroy');
                            try {
                                opts.handler.call(this, data);
                            } catch (e) {
                            }
                        } else {
                            alert(msg);
                        }
                    }
                });
                formObj.submit();
            }
        }, {
            text: ' 取消 ',
            iconCls: 'icon-cancel',
            handler: function () {
                dgWidows.dialog('destroy');
            }
        }],
        toolbar: false
    }, options);
    opts.cache = false; //强制不缓存数据
    dgWidows.dialog(opts);
    return dgWidows;
};






